<?php
class Api
{
    function startup($config = array())
    {
        /* 加载初始化文件 */
        require(ROOT_PATH . '/eccore/controller/app.base.php'); //基础控制器类
        require(ROOT_PATH . '/eccore/model/model.base.php'); //模型基础类

        /* 如果未定义主库 */
        if (!defined('MASTER_DB_CONFIG')) {
            define('MASTER_DB_CONFIG', DB_CONFIG);
        }

        if (!empty($config['external_libs'])) {
            foreach ($config['external_libs'] as $lib) {
                require($lib);
            }
        }

        /* 数据过滤 */
        if (!get_magic_quotes_gpc()) {
            $_GET = addslashes_deep($_GET);
            $_POST = addslashes_deep($_POST);
            $_COOKIE = addslashes_deep($_COOKIE);
        }

        /* 请求转发 */
        $default_app = $config['default_app'] ? $config['default_app'] : 'default';
        $default_act = $config['default_act'] ? $config['default_act'] : 'index';

        $app = isset($_REQUEST['app']) ? trim($_REQUEST['app']) : $default_app;
        $act = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : $default_act;
        $app_file = $config['app_root'] . "/{$app}.app.php";
        if (!is_file($app_file)) {
            $json_str = call_user_func_array('ApiError::showError', array('Missing Controller "' . $app . '"', '001'));
            exit($json_str);
        }

        require($app_file);
        define('APP', $app);
        define('ACT', $act);
        $app_class_name = ucfirst($app) . 'App';

        /* 实例化控制器 */
        $app = new $app_class_name();
        c($app);
        $app->do_action($act); //转发至对应的Action
        $app->destruct();
    }
}